﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Common;
using LogLib;
using Newtonsoft.Json.Linq;

namespace Reader
{
    public class IDCardReaderS : DeviceBase
    {
        private ReaderDll _readerDll = new ReaderDll();
        private string _inParamString = "";
        private bool _cancellToken = false;

        public IDCardReaderS()
        {

        }

        private void OnReading()
        {
            try
            {
                _cancellToken = false;
                JObject inParam = JObject.Parse(_inParamString);
                string phtoPath = (string)inParam["photoPath"];
                JObject outParam = null;
                string outParamString = "";
                while (!_cancellToken)
                {
                    ReaderDll.STR_ID_INFO id_info = new ReaderDll.STR_ID_INFO(); ;
                    int ret = _readerDll.ReadIdCard(phtoPath, out id_info);
                    if (0 == ret)
                    {
                        outParam = new JObject();
                        outParam.Add("ZJHM", id_info.idNum);
                        outParam.Add("XM", id_info.name);
                        outParam.Add("XB", id_info.sex);
                        outParam.Add("MZ", id_info.people);
                        outParam.Add("DZ", id_info.address);
                        outParam.Add("CSRQ", id_info.birth);
                        outParam.Add("QFDW", id_info.issuePlace);
                        outParam.Add("QFSJ", id_info.issueDay);
                        outParam.Add("SXSJ", id_info.expiryDay);
                        outParamString = outParam.ToString();
                        // 处理完成消息
                        OnWorkingDone(outParamString);
                        return;
                    }
                    Thread.Sleep(500);
                }
                _state = DeviceState.Readying;
            }
            catch (Exception ex)
            {
                OnCatchException(DeviceAction.Start, ex);
            }         
        }

        public override void Start(string param)
        {
            _state = DeviceState.Running;
            _inParamString = param;
            Thread thread = new Thread(OnReading);
            thread.Start();
            OnStartingOK("");
        }

        public override void Stop(string param)
        {
            _cancellToken = true;
            SpinWait.SpinUntil(()=> _state == DeviceState.Readying);
            OnStopOK("");
        }

        public override void Test(string param)
        {
            OnTestOK("");
        }

        public override void Init(string param)
        {
            _state = DeviceState.Initializing;           
            Log.Write("Start Init");
            int ret = _readerDll.InitReader();
            if (0 == ret)
            {
                _bInited = true;
                Log.Write("Init Success");
                _state = DeviceState.Readying;
                OnInitOK();
            }
            else
            {
                _bInited = false;
                _state = DeviceState.NotInitialized;
                Log.Write("Init Fail:" + ret.ToString());
                OnInitFailed("Init Fail:" + ret.ToString(), "");
            }
        }

        public override void DoAction(string param)
        {
            Log.Write("Start DoPerso");
            JObject inParam = JObject.Parse(param);
            string phtoPath = (string)inParam["photoPath"];
            JObject outParam = null;
            ReaderDll.STR_ID_INFO id_info = new ReaderDll.STR_ID_INFO(); ;
            int ret = _readerDll.ReadIdCard(phtoPath, out id_info);
            if (0 == ret)
            {
                outParam = new JObject();
                outParam.Add("ZJHM", id_info.idNum);
                outParam.Add("XM", id_info.name);
                outParam.Add("XB", id_info.sex);
                outParam.Add("MZ", id_info.people);
                outParam.Add("DZ", id_info.address);
                outParam.Add("CSRQ", id_info.birth);
                outParam.Add("QFDW", id_info.issuePlace);
                outParam.Add("QFSJ", id_info.issueDay);
                outParam.Add("SXSJ", id_info.expiryDay);
                Log.Write("DoPerso success");
                OnActionDone(outParam.ToString());
            }
            else
            {               
                Log.Write("DoPerso Fail:" + ret.ToString());
                OnActionFailed("DoPerso Fail:" + ret.ToString(), "");
            }
        }

        public override void Exit(string inParams)
        {
            if (_bInited)
            {
                _state = DeviceState.Exiting;
                _readerDll.UnInitReader();
                _bInited = false;
                _bExited = true;
                _state = DeviceState.NotInitialized;
            }
            Log.Write("Device has exited.");
            // 向客户端发送状态
            OnExitOK("");
        }
    }
}
